import { getOrganUserAll } from "api/seting/user";

var members = []
export default {
    created() {
        this.getMembers();
    },
    methods: {
        getMembers() {//获取员工
            getOrganUserAll().then(res => {
                members = res.data.organUsers;
                this.options = this.generateOptions(res.data.organs);
            });
        },
        generateOptions(datas) {
            //生成树状数据
            var result = [];
            for (let data of datas) {
                if (data.parentCode == "0") {
                    //判断是否为顶层节点
                    let parent = {
                        label: data.organName,
                        id: data.organCode,
                    };
                    parent.children = this.getUsers(data.id, members);
                    parent.children.push(...this.getchilds(data.organCode, datas)); //获取子节点
                    result.push(parent);
                }
            }
            return result;
        },
        getchilds(code, array) {
            let childs = new Array();
            for (let arr of array) {
                //循环获取子节点
                if (arr.parentCode == code) {
                    let child = {
                        id: arr.organCode,
                        label: arr.organName,
                        _id: arr.id
                    };
                    child.children = this.getUsers(arr.id, members);
                    childs.push(child);
                }
            }
            for (let child of childs) {
                //获取子节点的子节点
                let childscopy = this.getchilds(child._id, array); //递归获取子节点
                if (childscopy.length > 0) {
                    child.children.push(...childscopy);
                }
            }
            return childs;
        },
        // 获取成员
        getUsers(id, members) {
            let users = new Array();
            for (let member of members) {
                //循环获取子节点
                if (member.organId == id) {
                    users.push(
                        ...member.users.map(o => {
                            return {
                                id: o.id,
                                label: o.realName
                            };
                        })
                    );
                }
            }
            return users;
        }
    }
}
